package com.crazypos.exceptions;

import com.crazypos.common.ApiRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /***
     * 处理通用异常
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ApiRestResponse handleException(Exception ex) {
        log.error("DEFAULT EXCEPTION: [{}]", ex);
        return ApiRestResponse.error(500, ex.getMessage());
    }

    /***
     * 捕获MyException
     * @param myException
     * @return
     */
    @ExceptionHandler(MyException.class)
    public ApiRestResponse handleMyException(MyException myException) {
        log.error("MYEXCEPTION : [{}], [{}]", myException.getCode(), myException.getErrorMsg());
        return ApiRestResponse.error(myException.getCode(), myException.getErrorMsg());
    }

}
